#include <REGX52.H>
 
//LCD????
sbit LCD_RS=P3^5;
sbit LCD_RW=P3^6;
sbit LCD_EN=P3^7;
#define LCD_DataPort P2
 
unsigned char Hour=23,Min=59,Sec=55;
//LCD????
void Delay()
{
	unsigned char i, j;
 
	i = 2;
	j = 239;
	do
	{
		while (--j);
	} while (--i);
}
 
//LCD???
void LCD_WriteCommand(unsigned char Command)
{
	LCD_RS=0;
	LCD_RW=0;
	LCD_DataPort=Command;
	LCD_EN=1;
	Delay();
	LCD_EN=0;
	Delay();
}
 
//LCD???
void LCD_WriteData(unsigned char Data)
{
	LCD_RS=1;
	LCD_RW=0;
	LCD_DataPort=Data;
	LCD_EN=1;
	Delay();
	LCD_EN=0;
	Delay();
}
 
//LCD?????? 
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
	if(Line==1)
	{
		LCD_WriteCommand(0x80|(Column-1));
	}
	else if(Line==2)
	{
		LCD_WriteCommand(0x80|(Column-1+0x40));
	}
}
 
//LCD?????
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
	unsigned char i;
	LCD_SetCursor(Line,Column);
	for(i=0;String[i]!='\0';i++)
	{
		LCD_WriteData(String[i]);
	}
}
 
//LCD????
int LCD_Pow(int X,int Y)
{
	unsigned char i;
	int Result=1;
	for(i=0;i<Y;i++)
	{
		Result*=X;
	}
	return Result;
}
 
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
	unsigned char i;
	LCD_SetCursor(Line,Column);
	for(i=Length;i>0;i--)
	{
		LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
	}
}
 
//LCD???
void LCD_Init()
{
	LCD_WriteCommand(0x38);//??????,????,5*7??
	LCD_WriteCommand(0x0c);//???,???,???
	LCD_WriteCommand(0x06);//???????,??????,????
	LCD_WriteCommand(0x01);//????,??
}
 
 
//??????
void Timer0_Init()
{
	TMOD=0x01;
	TH0=0xFc;
	TL0=0x66;
	TF0=0;
	TR0=1;
	ET0=1;
	EA=1;
}
 
void main()
{
	LCD_Init();
	Timer0_Init();
	LCD_ShowString(1,1,"  :  :  ");
	while(1)
	{
 
	LCD_ShowNum(1,1,Hour,2);
	LCD_ShowNum(1,4,Min,2);
	LCD_ShowNum(1,7,Sec,2);
	}
}
 
void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TH0=0xFc;
	TL0=0x66;
	T0Count++;
	if(T0Count==1000)
	{
		Sec++;
		T0Count=0;
	}
	if(Sec>=60)
	{
		Sec=0;
		Min++;
	}
	if(Min>=60)
	{
		Min=0;
		Hour++;
	}
	if(Hour>=24)
	{
		Hour=0;
	}
}
	